<?php

namespace app\admin\controller\kuerp;

use app\admin\model\kuerp\CollectionSale;
use app\common\controller\Backend;
use think\Db;
use Exception;
use think\Session;
/**
 * 收款单
 *
 * @icon fa fa-circle-o
 */
class Collection extends Backend
{

    /**
     * Collection模型对象
     * @var \app\admin\model\kuerp\Collection
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $setData = (new Goods())->getSetData();
        //不需要审核20240930
        $setData['setList']['examine']['is_checked'] = 0;
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set',$setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $this->model = new \app\admin\model\kuerp\Collection;
        $this->view->assign("examineStatusList", $this->model->getExamineStatusList());
        // $this->view->assign("customers", $this->model->getCustomersList());
        $this->view->assign("accountsList", $this->model->getAccountsList());
        $this->view->assign("saleList", $this->model->getSaleList());
        $this->view->assign("settlementtype", $this->model->getSettlementtype());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $paramWhere = [];
            if (!empty(input('purchase_time'))) {
                $purchase_time = str_replace(' - ', ',', input('purchase_time'));
                $arr = array_slice(explode(',', $purchase_time), 0, 2);
                $paramWhere['purchase_time'] = ['between time', $arr];
            }
            $list = $this->model
                ->with(['admin','staffmember'])
                ->where($where)
                ->where($paramWhere)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->getRelation('admin')->visible(['username', 'nickname']);
                $row->getRelation('staffmember')->visible(['name']);
            }
            $lists = [];
            $moneys = 0;$write_off = 0;
            $customers_list = [];
            $admin_list = [];
            if ($list) {
                $customers_list = Db::name('kuerp_customers')->where(['id'=>['in', array_column($list->items(), 'customers_id')]])->column('customer_name', 'id');
                $admin_list = Db::name('admin')->where(['id'=>['in', array_column($list->items(), 'examine_id')]])->column('username,nickname', 'id');
            }
            foreach ($list as $list_k=>$list_v){
                if(isset($list_v['print_num']) && empty($list_v['print_num'])){
                    $list_v['print_num'] = 0;
                }
                $customers = $customers_list[$list_v['customers_id']] ?? '';//Db::name('kuerp_customers')->where(['id'=>$list_v['customers_id']])->value('customer_name');
                $staff_member = $list_v['staffmember']['name'];//Db::name('kuerp_staff_member')->where(['id'=>$list_v['staff_member_id']])->value('name');
                $preparation = $list_v['admin']['nickname'];//Db::name('admin')->where(['id'=>$list_v['preparation_id']])->value('username');
                $examine =  $admin_list[$list_v['examine_id']]['nickname'] ?? '';//Db::name('admin')->where(['id'=>$list_v['examine_id']])->value('username');

                $account = Db::name('kuerp_collection_account')->alias('co')
                    ->join('kuerp_accounts a','a.id=co.accounts_id')
                    ->join('kuerp_settlementtype s','s.id=co.settlementtype','left')
                    ->where((new Goods())->setWhere(['co.collection_id'=>$list_v['id']]))
                    ->field('co.money,a.account_name,s.name,co.number,co.remark')
                    ->select();
                $moneys += $list_v['money'];$write_off += $list_v['write_off'];
                foreach ($account as $account_k=>$account_v){
                    $lists[] = [
                        'id'            => $list_v['id'],
                        'purchase_code' => $list_v['purchase_code'],
                        'purchase_time' => date('Y-m-d',$list_v['purchase_time']),
                        'customers_id'  => $customers,
                        'accounts_id'   => $account_v['account_name'],
                        'money'         => $account_v['money'],
                        'settlementtype'=> $account_v['name'],
                        'number'        => $account_v['number'],
                        'remark'        => $account_v['remark'],
                        'moneys'        => $list_v['money'],
                        'write_off'     => $list_v['write_off'],
                        'discount'      => $list_v['discount'],
                        'advance'       => $list_v['advance'],
                        'staff_member'  => $staff_member,
                        'examine'       => $examine,
                        'preparation'   => $preparation,
                        'remarks'       => $list_v['remarks'],
                        'print_num'     => $list_v['print_num'],
                        'createtime'    => date('Y-m-d H:i', $list_v['createtime']),
                    ];
                }
            }

            $result = array("total" => $list->total(), "rows" => $lists, "moneys" => $moneys, "write_off" => $write_off);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 导出信息
     */
    public function export()
    {
        $param = $this->request->param();
        if (isset($param['user_path'])) {
            if (file_exists($_SERVER['DOCUMENT_ROOT'].$param['user_path'])) {
                unlink($_SERVER['DOCUMENT_ROOT'].$param['user_path']); // 删除文件
            }
            exit();
        }
        set_time_limit(0);
        ini_set('memory_limit', '1024M');
        $ids = $param['ids'];
        $isall = $param['isall'];
        $wheres = [];
        if($isall != 3 && empty($ids)){
            $this->error(__('请选择要导出的数据'));
        }
        //导出其中几条
        if (isset($ids) ) {
            $wheres['collection_id'] = array('in', $ids);
        }
        //导出全部
        if (isset($isall)) {
            if ($isall == 3) {
                unset($wheres['collection_id']);
            }
        }
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        if (!empty(input('purchase_time'))) {
            $purchase_time = str_replace(' - ', ',', input('purchase_time'));
            $arr = array_slice(explode(',', $purchase_time), 0, 2);
            $wheres['purchase_time'] = ['between time', $arr];
        }

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = (new CollectionSale())
            ->with(['collection','goodssales','goodssalesinfo'])
            ->where($where)
            ->where($wheres)
            ->order($sort, $order)
            ->select();

        foreach ($list as $row) {
            $row->getRelation('collection')->visible(['customers_id', 'staff_member_id']);
            $row->getRelation('goodssalesinfo')->visible(['goods_code','goods_type','number','unit_price','back_money','all_money','remark','period','sale_user_name']);
        }
        $list = collection($list)->toArray();
        if (!$list) {
            $this->error('无导出数据');
        }
        $goods_code = array_unique(array_map(function($item) {
            return $item['goodssalesinfo']['goods_code'];
        }, $list));
        $good = Db::name('kuerp_goods')->alias('a')
            ->join('kuerp_unit b','a.unit_id=b.id','left')
            ->where('a.goods_code','in',$goods_code)
            ->column('a.goods_name,b.name as unit_name','a.goods_code');

        $customers_id = array_unique(array_map(function($item) {
            return $item['collection']['customers_id'];
        }, $list));
        $customers = Db::name('kuerp_customers')->whereIn('id',$customers_id)
            ->column('customer_name,connet_name,customer_mobile,customer_code','id');
        //获取二级审核描述
        $orderids = array_unique(array_map(function($item) {
            return $item['goodssales']['id'];
        }, $list));
        $examines = Db::name('examine_log')->where(['relation_type'=>'goods_sale','examine_status' => 1, 'cur_index' => 2])->whereIn('relation_id',$orderids)->column('remark','relation_id');
        $staff_members = Db::name('kuerp_staff_member')->column('name','id');
        $title = [
            '客户名称',
            '客户编号',
            '销售人员',
            '收款人',
            '回款编号',
            '本次回款金额',
            '回款日期',
            '源单编号',
            '产品总金额',
            '销售金额',
            '返款金额',
            '已收款金额',
            '锁编码',
            '锁内码',
            '购买类型',
            '购买版本',
            '订单备注',
            '创建时间',
            '联系人',
            '联系人手机',
            '审核备注',
            '商品名称',
            '商品编号',
            '商品数量',
            '商品单位',
            '购买类型',
            '销售单价',
            '返款金额',
            '销售金额',
            '锁号',
            '商品备注',
        ];

        $orderinfo = [];
        foreach ($list as $k => $v) {
            $serial = '';
            if (!empty($v['goodssalesinfo']['period'])) {
                $period = unserialize($v['goodssalesinfo']['period']);
                foreach ($period as $k1=>$v1) {
                    $serial .= $v1['serial'].'-'.$v1['remark'].',';
                }
                $serial = rtrim($serial, ',');
            }
            $field1 = array(
                $customers[$v['collection']['customers_id']]['customer_name'] ?? '',
                $customers[$v['collection']['customers_id']]['customer_code'] ?? '',
                $v['goodssalesinfo']['sale_user_name'] ?? '',
                $staff_members[$v['collection']['staff_member_id']] ?? '',
                $v['purchase_code'] ?? '',
                $v['write_off_price'] ?? '',
                date('Y-m-d',$v['purchase_time']),
                $v['purchase_number'],
                round(($v['goodssales']['money_tax_price'] + $v['goodssales']['back_money']) * 100) / 100,
                $v['goodssales']['all_money'],
                $v['goodssales']['back_money'] ?? 0,
                $v['goodssales']['pay_money'] ?? 0,
                $v['goodssales']['serials'],
                $v['goodssales']['lock_code'],
                $v['goodssales']['buy_type'] ?? '',
                $v['goodssales']['buy_version'] ?? '',
                $v['goodssales']['note'] ?? '',
                date('Y-m-d H:i:s',$v['goodssales']['createtime']),
                $customers[$v['collection']['customers_id']]['connet_name'] ?? '',
                $customers[$v['collection']['customers_id']]['customer_mobile'] ?? '',
                $examines[$v['goodssales']['id']] ?? ''
            );

            $field2 = array(
                $good[$v['goodssalesinfo']['goods_code']]['goods_name'] ?? '',
                $good[$v['goodssalesinfo']['goods_code']]['goods_code'] ?? '',
                $v['goodssalesinfo']['number'] ?? 0,
                $good[$v['goodssalesinfo']['goods_code']]['unit_name'] ?? '',
                $v['goodssalesinfo']['goods_type'] ?? '',
                $v['goodssalesinfo']['unit_price'] ?? 0,
                $v['goodssalesinfo']['back_money'] ?? 0,
                $v['goodssalesinfo']['all_money'] ?? 0,
                $serial,
                $v['goodssalesinfo']['remark'] ?? '',
            );

            if(isset($orderinfo[$v['purchase_number']]) && $orderinfo[$v['purchase_number']] == $v['purchase_code']){
                $field1 = array_fill(0, count($field1), '');
            }
            $field = array_merge($field1, $field2);
            $orderinfo = [];
            $orderinfo[$v['purchase_number']] =  $v['purchase_code'];
            $data[] = $field;
        }

        $file = export_excel($title, $data, '回款记录');
        if ($file['filePath']) {
            $this->success('导出成功', '', $file);
        }
        $this->error('导出失败');
    }

    public function add($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                (new \app\admin\controller\kuerp\Checkout())->checkOutTime($params['purchase_time']);
                if(empty($params['customers_id'])){
                    $this->error('请选择客户');
                }
                //收款单不需要审核20240930
                $examine= 0;//Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');
                $params = $this->preExcludeFields($params);

                if(empty($params['purchase_code'])){
                    $this->error('请填写单据编号');
                }
                $admin = Session::get('admin');

                //账户信息
                $account = [];
                $account_id = [];
                foreach ($params['accounts_id'] as $k=>$v){
                    if(!empty($v)){//有账户
                        $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$v]))->find();
                        if(empty($accounts)){
                            $this->error('账户信息错误');
                        }
                        if(in_array($v,$account_id)){
                            $this->error('不可多次选择相同的结算账户');
                        }else{
                            $account_id[] = $v;
                        }
                        $account[] = (new Goods())->setDataInfo([
                            'purchase_code'    => $params['purchase_code'],
                            'purchase_time'    => strtotime($params['purchase_time']),
                            'accounts_id'      => $v,
                            'money'            => isset($params['money'][$k])?(float)$params['money'][$k]:0,
                            'settlementtype'   => isset($params['settlementtype'][$k])?(float)$params['settlementtype'][$k]:0,
                            'number'           => isset($params['number'][$k])?$params['number'][$k]:'',
                            'remark'           => isset($params['remark'][$k])?$params['remark'][$k]:null,
                        ]);
                    }
                }
                $account_money = 0;//账户收支总金额
                foreach ($account as $account_k=>$account_v){
                    $account_money += $account_v['money'];
                }
                //账户信息结束
                //源单信息
                $sale = [];
                $sale_id = [];
                $log_arr = [];
                $log_add = [];
                foreach ($params['purchase_number'] as $k=>$v){
                    if(!empty($v) && !empty($params['purchase_type'][$k])){//有单号
                        $order = Db::name('kuerp_writeoff_source')->where(['id'=>$params['purchase_type'][$k]])->find();
                        if(empty($order)){
                            $this->error('源单信息查询错误');
                        }
                        if(in_array($v,$sale_id)){
                            $this->error('不可多次选择相同的源单');
                        }else{
                            $sale_id[] = $v;
                        }
                        $thisrow = (new Goods())->setDataInfo([
                            'purchase_code'    => $params['purchase_code'],
                            'purchase_time'    => strtotime($params['purchase_time']),
                            'purchase_number'  => $order['purchase_code'],
                            'business_name'    => isset($params['business_type'][$k])?$params['business_type'][$k]:null,
                            'business_type'    => $order['purchase_type'],
                            'times'            => $order['purchase_time'],
                            'money'            => $order['price'],
                            'write_off'        => isset($params['write_off'][$k])?(float)$params['write_off'][$k]:0,
                            'write_off_money'  => isset($params['write_off_money'][$k])?(float)$params['write_off_money'][$k]:0,
                            'write_off_price'  => isset($params['write_off_price'][$k])?(float)$params['write_off_price'][$k]:0,
                            'remark'           => $order['remark'],
                            'source_id'        => $order['id'],
                            'type'             => $order['type'],
                        ]);
                        $sale[] = $thisrow;

                        $where = [];
                        $where['source_id'] = $order['id'];
                        $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere($where))->find();
                        if($log){
                            $status = true;
                            foreach ($log_arr as $log_arr_k=>$log_arr_v){
                                if($log_arr_v['id']==$log['id']){
                                    $status = false;
                                    $log_arr[$log_arr_k]['money'] += $thisrow['write_off_price'];
                                    $log_arr[$log_arr_k]['write_off'] -= $thisrow['write_off_price'];
                                }
                            }
                            if($status){
                                $log_arr[] = [
                                    'id' => $log['id'],
                                    'write_off' => $log['write_off']-$thisrow['write_off_price'],
                                    'money' => $log['money']+$thisrow['write_off_price'],
                                ];
                            }
                        }else{
                            $log_add[] = [
                                'source_id' => $order['id'],
                                'write_off' => $thisrow['write_off_money']-$thisrow['write_off_price'],
                                'money' => $thisrow['write_off_price'],
                            ];
                        }
                    }
                }
                $sale_money = 0;//账户收支总金额
                foreach ($sale as $sale_k=>$sale_v){
                    $sale_money += $sale_v['write_off_price'];
                }

                if ($account_money != $sale_money) {
                    $this->error('收款金额与本次核销金额不相等');
                }

                $business_type = !empty($sale)?2:1;
                $row = (new Goods())->setDataInfo([
                    'purchase_code'     =>  $params['purchase_code'],
                    'purchase_time'     =>  strtotime($params['purchase_time']),
                    'business_type'     => $business_type,
                    'customers_id'      =>  $params['customers_id'],
                    'money'             =>  $account_money,
                    'write_off'         =>  $sale_money,
                    'discount'          =>  isset($params['discount'])?(float)$params['discount']:0,
                    'advance'           =>  isset($params['advance'])?(float)$params['advance']:0,
                    'staff_member_id'   =>  isset($params['staff_member_id'])?(float)$params['staff_member_id']:null,
                    'remarks'           =>  $params['note'],
                    'preparation_id'    =>  $admin['id'],
                    'operation_id'      =>  $admin['id'],
                    'createtime'        => time(),
                    'updatetime'        => time(),
                ]);

                if(empty($account)){
                    $this->error('请填写账户结算信息');
                }
                if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code'])]))->find()){
                    $this->error('单据编号已占用');
                }

                $result = false;
                Db::startTrans();
                try {
                    if(!empty($row)){
                        $result = $this->model->insertGetId($row);
                    }

                    foreach ($account as $account_k=>$account_v){
                        $account[$account_k]['collection_id'] = $result;
                    }
                    Db::name('kuerp_collection_account')->insertAll($account);

                    foreach ($sale as $sale_k=>$sale_v){
                        $sale[$sale_k]['collection_id'] = $result;
                    }
                    Db::name('kuerp_collection_sale')->insertAll($sale);

                    if(!empty($log_arr)){
                        (new \app\admin\model\kuerp\WriteoffLog())->saveAll($log_arr);
                    }
                    if(!empty($log_add)){
                        (new \app\admin\model\kuerp\WriteoffLog())->insertAll($log_add);
                    }
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if (isset($result) && $result !== false) {
                    if($examine!=1){
                        try {
                            $exa = $this->examine($result,1);
                        } catch (Exception $e) {
                        }
                        if(!isset($exa) || $exa['status']!=1){
                            $exa = $this->examine($result,1);
                            try {
                                $this->del($result);
                            } catch (Exception $e) {
                            }
                            $this->error($exa['msg']);
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $code = (new Goods())->getCodes('',21);
        $this->view->assign("code", $code);
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            (new \app\admin\controller\kuerp\Checkout())->checkOutTime($row['purchase_time']);
            //收款单不需要审核20240930
            $examine= 0;//Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');
            if($row['examine_status']==1 && $examine==1){
                $this->error('不能编辑已审核的数据');
            }

            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                if(empty($params['customers_id'])){
                    $this->error('请选择客户');
                }
                if(empty($params['purchase_code'])){
                    $this->error('请填写单据编号');
                }
                $admin = Session::get('admin');

                //账户信息
                $account = [];
                $account_id = [];
                foreach ($params['accounts_id'] as $k=>$v){
                    if(!empty($v)){//有账户
                        $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$v]))->find();
                        if(empty($accounts)){
                            $this->error('账户信息错误');
                        }
                        if(in_array($v,$account_id)){
                            $this->error('不可多次选择相同的结算账户');
                        }else{
                            $account_id[] = $v;
                        }
                        $account[] = (new Goods())->setDataInfo([
                            'purchase_code'    => $params['purchase_code'],
                            'purchase_time'    => strtotime($params['purchase_time']),
                            'collection_id'    => $ids,
                            'accounts_id'      => $v,
                            'money'            => isset($params['money'][$k])?(float)$params['money'][$k]:0,
                            'settlementtype'   => isset($params['settlementtype'][$k])?(float)$params['settlementtype'][$k]:0,
                            'number'           => isset($params['number'][$k])?$params['number'][$k]:'',
                            'remark'           => isset($params['remark'][$k])?$params['remark'][$k]:null,
                        ]);
                    }
                }
                $account_money = 0;//账户收支总金额
                foreach ($account as $account_k=>$account_v){
                    $account_money += $account_v['money'];
                }
                //账户信息结束

                //源单信息
                $sale = [];
                $sale_id = [];
                $log_arr = [];
                $log_add = [];
                foreach ($params['purchase_number'] as $k=>$v){
                    if(!empty($v) && !empty($params['purchase_type'][$k])){//有单号
                        $order = Db::name('kuerp_writeoff_source')->where(['id'=>$params['purchase_type'][$k]])->find();
                        if(empty($order)){
                            $this->error('源单信息查询错误');
                        }
                        if(in_array($v,$sale_id)){
                            $this->error('不可多次选择相同的源单');
                        }else{
                            $sale_id[] = $v;
                        }
                        $thisrow = (new Goods())->setDataInfo([
                            'purchase_code'    => $params['purchase_code'],
                            'purchase_time'    => strtotime($params['purchase_time']),
                            'purchase_number'  => $order['purchase_code'],
                            'collection_id'    => $ids,
                            'business_name'    => isset($params['business_type'][$k])?$params['business_type'][$k]:null,
                            'business_type'    => $order['purchase_type'],
                            'times'            => $order['purchase_time'],
                            'money'            => $order['price'],
                            'write_off'        => isset($params['write_off'][$k])?(float)$params['write_off'][$k]:0,
                            'write_off_money'  => isset($params['write_off_money'][$k])?(float)$params['write_off_money'][$k]:0,
                            'write_off_price'  => isset($params['write_off_price'][$k])?(float)$params['write_off_price'][$k]:0,
                            'remark'           => $order['remark'],
                            'source_id'        => $order['id'],
                            'type'             => $order['type'],
                        ]);
                        $sale[] = $thisrow;

                        $where = [];
                        $where['source_id'] = $order['id'];
                        $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere($where))->find();
                        if($log){
                            $status = true;
                            foreach ($log_arr as $log_arr_k=>$log_arr_v){
                                if($log_arr_v['id']==$log['id']){
                                    $status = false;
                                    $log_arr[$log_arr_k]['money'] += $thisrow['write_off_price']-(float)$params['write_off_prices'][$k];
                                    $log_arr[$log_arr_k]['write_off'] -= $thisrow['write_off_price']+(float)$params['write_off_prices'][$k];
                                }
                            }
                            if($status){
                                $log_arr[] = [
                                    'id' => $log['id'],
                                    'write_off' => $log['write_off']-$thisrow['write_off_price']+(float)$params['write_off_prices'][$k],
                                    'money' => $log['money']+$thisrow['write_off_price']-(float)$params['write_off_prices'][$k],
                                ];
                            }
                        }else{
                            $log_add[] = [
                                'source_id' => $order['id'],
                                'write_off' => $thisrow['write_off_money']-$thisrow['write_off_price']+(float)$params['write_off_prices'][$k],
                                'money' => $thisrow['write_off_price']-(float)$params['write_off_prices'][$k],
                            ];
                        }
                    }
                }
                $sale_money = 0;//账户收支总金额
                foreach ($sale as $sale_k=>$sale_v){
                    $sale_money += $sale_v['write_off_price'];
                }
                if ($account_money != $sale_money) {
                    $this->error('收款金额与本次核销金额不相等');
                }
                $business_type = !empty($sale)?2:1;
                $rowdata = (new Goods())->setDataInfo([
                    'purchase_code'     =>  $params['purchase_code'],
                    'purchase_time'     =>  strtotime($params['purchase_time']),
                    'customers_id'      =>  $params['customers_id'],
                    'business_type'     => $business_type,
                    'money'             =>  $account_money,
                    'write_off'         =>  $sale_money,
                    'discount'          =>  isset($params['discount'])?(float)$params['discount']:0,
                    'advance'           =>  isset($params['advance'])?(float)$params['advance']:0,
                    'staff_member_id'   =>  isset($params['staff_member_id'])?(float)$params['staff_member_id']:null,
                    'remarks'           =>  $params['note'],
                    'preparation_id'    =>  $admin['id'],
                    'operation_id'      =>  $admin['id'],
                    'updatetime'        => time(),
                ]);

                if(empty($account)){
                    $this->error('请填写账户结算信息');
                }

                if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code']),'id'=>['<>',$ids]]))->find()){
                    $this->error('单据编号已占用');
                }

                //详情
                $info_list = Db::name('kuerp_writeoff_info')->where((new Goods())->setWhere(['collection_id'=>$ids]))->select();
                $writeoff_log = [];
                foreach ($info_list as $info_list_k=>$info_list_v){
                    $log = Db::name('kuerp_writeoff_log')->where(['source_id'=>$info_list_v['source_id']])->find();
                    if($log){
                        $addstatus = true;
                        foreach ($writeoff_log as $writeoff_log_k=>$writeoff_log_v){
                            if($writeoff_log_v['id']==$log['id']){
                                $addstatus = false;
                                $writeoff_log[$writeoff_log_k]['write_off'] = $writeoff_log_v['write_off']+$info_list_v['write_off_price'];
                                $writeoff_log[$writeoff_log_k]['money'] = $writeoff_log_v['money']-$info_list_v['money'];
                            }
                        }
                        if($addstatus){
                            $writeoff_log[] = [
                                'id' => $log['id'],
                                'write_off'=>($log['write_off']+$info_list_v['write_off_price']),
                                'money'=>($log['money']-$info_list_v['write_off_price'])
                            ];
                        }
                    }
                }
                foreach ($log_arr as $log_arr_k=>$log_arr_v){
                    $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere(['id'=>$log_arr_v['id']]))->find();
                    if($log){
                        $addstatus = true;
                        foreach ($writeoff_log as $writeoff_log_k=>$writeoff_log_v){
                            if($writeoff_log_v['id']==$log['id']){
                                $addstatus = false;
                                $writeoff_log[$writeoff_log_k]['write_off'] = $writeoff_log_v['write_off']-$log_arr_v['write_off'];
                                $writeoff_log[$writeoff_log_k]['money'] = $writeoff_log_v['money']+$log_arr_v['money'];
                            }
                        }
                        if($addstatus){
                            $writeoff_log[] = [
                                'id' => $log['id'],
                                'write_off'=>($log['write_off']-$log_arr_v['write_off']),
                                'money'=>($log['money']+$log_arr_v['write_off'])
                            ];
                        }
                    }
                }
                $result = false;
                Db::startTrans();
                try {
                    if($examine!=1 && $row['examine_status']==1){
                        $this->examine($ids,2);
                    }
                    if(!empty($row)){
                        $result = $this->model->where((new Goods())->setWhere(['id'=>$ids]))->update($rowdata);
                    }
                    Db::name('kuerp_collection_account')->where((new Goods())->setWhere(['collection_id'=>$ids]))->delete();
                    Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['collection_id'=>$ids]))->delete();

                    Db::name('kuerp_collection_account')->insertAll($account);
                    Db::name('kuerp_collection_sale')->insertAll($sale);

                    if(!empty($log_add)){
                        (new \app\admin\model\kuerp\WriteoffLog())->insertAll($log_add);
                    }
                    if(!empty($writeoff_log)){
                        (new \app\admin\model\kuerp\WriteoffLog())->saveAll($writeoff_log);
                    }
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    if($examine!=1){
                        try {
                            $exa = $this->examine($ids,1);
                        } catch (Exception $e) {
                        }
                        if(!isset($exa) || $exa['status']!=1){
                            $this->error(__('No rows were inserted'));
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);

        $account = Db::name('kuerp_collection_account')->where((new Goods())->setWhere(['collection_id'=>$row['id']]))->select();
        $this->view->assign("account", $account);

        $sale = Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['collection_id'=>$row['id']]))->select();
        if ($sale) {
            $purchase_numbers = array_column($sale, 'purchase_number');
            $goodssales = Db::name('kuerp_goods_sales')->where('purchase_code', 'in', $purchase_numbers)->column('back_money','purchase_code');
            foreach ($sale as $sale_k=>$sale_v) {
                $sale[$sale_k]['back_money'] = $goodssales[$sale_v['purchase_number']];
            }
        }
        $this->view->assign("sale", $sale);

        $give_account = Db::name('kuerp_customers')->where(['id'=>$row['customers_id']])->value('need_receive_account');
        if($row['examine_status']==1){
            $this->assignconfig("give_account", $give_account-$row['money']);
            $this->view->assign("give_account", (float)$give_account);
        }else{
            $this->assignconfig("give_account", $give_account);
            $this->view->assign("give_account", $give_account);
        }

        $kuerp_customers = Db::name('kuerp_customers')->where(['id'=>$row['customers_id']])->field('id,customer_name')->find();
        $this->assignconfig("kuerp_customers", $kuerp_customers);

        //审核禁止输入 不需要审核20240930
        $examine_set= 0;//Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');
        if($examine_set==1 && $row['examine_status']==1){
            $this->assign('exclass','hiddens');
        }
        $this->assignconfig("examine_status", $row['examine_status']);

        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            //不需要审核20240930
            $examine= 0;//Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $del_id = [];
            foreach ($list as $list_k=>$list_v){
                if($list_v['examine_status']==1){
                    (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);
                }
                if($list_v['examine_status']==1 && $examine==1){
                    $this->error('不能删除已审核的数据');
                }elseif ($list_v['examine_status']==1 && $examine!=1){
                    try {
                        $this->examine($list_v['id'],2);
                        $del_id[] = $list_v['id'];
                    } catch (Exception $e) {
                    }
                }else{
                    $del_id[] = $list_v['id'];
                }
            }
            $list = $this->model->where((new Goods())->setWhere(['id'=>['in',$del_id]]))->select();
            //详情
            $info_list = Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['collection_id'=>['in',$del_id]]))->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                }
                foreach ($info_list as $info_list_k=>$info_list_v){
                    $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere(['source_id'=>$info_list_v['source_id']]))->find();
                    if($log){
                        Db::name('kuerp_writeoff_log')->where(['id'=>$log['id']])->update([
                            'write_off'=>($log['write_off']+$info_list_v['write_off_price']),
                            'money'=>($log['money']-$info_list_v['write_off_price'])
                        ]);
                    }
                }
                Db::name('kuerp_collection_account')->where((new Goods())->setWhere(['collection_id'=>['in',$del_id]]))->delete();
                Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['collection_id'=>['in',$del_id]]))->delete();
                Db::commit();
            }catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    //审核/反审核
    public function examine($ids = null,$examine=1){
        //不需要审核20240930
        $examine_set= 0;//Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');
        if(empty($ids)){
            if($examine_set!=1){
                return ['status'=>0,'msg'=>'系统错误'];
            }
            $this->error('请选择要操作的数据');
        }
        $admin = Session::get('admin');
        if($examine==1){
            $list = Db::name('kuerp_collection')->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'0']))->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);
            }
            if(empty($list)){
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'系统错误'];
                }
                $this->error('请选择需要审核的数据');
            }
            $success_count = 0;
            foreach ($list as $list_k=>$list_v){
                //账户信息
                $accountData = [];$log = [];
                $account = Db::name('kuerp_collection_account')->where((new Goods())->setWhere(['collection_id'=>$list_v['id']]))->select();
                foreach ($account as $account_k=>$account_v){
                    $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$account_v['accounts_id']]))->find();
                    $log[] = [
                        'account_coding'    => $accounts['account_coding'],
                        'account_name'      => $accounts['account_name'],
                        'balance'           => $accounts['balance']+$account_v['money'],
                        'income'            => $account_v['money'],
                        'pay_time'          => $list_v['purchase_time'],
                        'bill_code'         => $account_v['purchase_code'],
                        'business_type'     => '7',
                        'customers_id'      => $list_v['customers_id'],
                        'payment'           => $list_v['staff_member_id'],
                        'remark'            => $list_v['remarks'],
                        'createtime'        => time(),
                        'updatetime'        => time(),
                    ];
                    $accountData[] = [
                        'balance'  => $account_v['money'],
                        'account'  => $accounts['id'],
                    ];
                }
                //账户信息结束

                $info_list = Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['collection_id'=>['in',$ids]]))->select();
                $suppliers = Db::name('kuerp_customers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->find();
                $gave_money = $suppliers['need_receive_account'] - $list_v['money'];

                foreach ($info_list as $info_list_k=>$info_list_v){
                    if($info_list_v['business_type']==3 && $info_list_v['type']==1){
                        //普通销售
                        $order = Db::name('kuerp_goods_sales')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();
                        if($order){
                            $all_money = $order['real_money'];
                            $pay_money = $order['pay_money']+$info_list_v['write_off_price'];
                            if($all_money<$pay_money){
                                if($examine_set!=1){
                                    return ['status'=>0,'msg'=>$info_list_v['purchase_number'].'核销金额不能大于未核销金额'];
                                }
                                $this->error($info_list_v['purchase_number'].'核销金额不能大于未核销金额');
                            }
                        }
                    }else if($info_list_v['business_type']==3 && $info_list_v['type']==2){
                        //销售退货
                        $order = Db::name('kuerp_goods_sales_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();
                        if($order){
                            $all_money = $order['real_money'];
                            $pay_money = $order['retreat_money']+(-$info_list_v['write_off_price']);
                            if($all_money<$pay_money){
                                if($examine_set!=1){
                                    return ['status'=>0,'msg'=>$info_list_v['purchase_number'].'核销金额不能大于未核销金额'];
                                }
                                $this->error($info_list_v['purchase_number'].'核销金额不能大于未核销金额');
                            }
                        }
                    }
                }

                //客户对账单
                if(empty($info_list)){
                    $accounts_receivable = [];
                    if(!empty($list_v['customers_id'])){
                        $accounts_receivable[] = (new Goods())->setDataInfo([
                            'customers_id'      => $list_v['customers_id'],
                            'purchase_time'     => $list_v['purchase_time'],
                            'purchase_code'     => $list_v['purchase_code'],
                            'business_type'     => '9',
                            'advance'           => $list_v['money'],
                            'remark'            => $list_v['remarks'],
                            'real_money'        => $list_v['money'],
                            'createtime'        => time(),
                            'updatetime'        => time(),
                        ]);
                    }
                }else{
                    $accounts_receivable = [];
                    if(!empty($list_v['customers_id'])){
                        $accounts_receivable[] = (new Goods())->setDataInfo([
                            'customers_id'      => $list_v['customers_id'],
                            'purchase_time'     => $list_v['purchase_time'],
                            'purchase_code'     => $list_v['purchase_code'],
                            'business_type'     => '9',
                            'receivable'        => -$list_v['money'],
                            'remark'            => $list_v['remarks'],
                            'real_money'        => $list_v['money'],
                            'createtime'        => time(),
                            'updatetime'        => time(),
                        ]);
                    }
                }

                Db::startTrans();
                try {

                    if($list_v['business_type']==1){
                        Db::name('kuerp_writeoff_source')->insert([
                            'purchase_type' => 1,
                            'type' => 1,
                            'customers_id' => $list_v['customers_id'],
                            'purchase_code' => $list_v['purchase_code'],
                            'purchase_time' => $list_v['purchase_time'],
                            'price' => $list_v['advance'],
                            'money' => 0,
                            'write_off' => $list_v['advance'],
                            'remark' => $list_v['remarks'],
                        ]);
                    }

                    foreach ($info_list as $info_list_k=>$info_list_v){
                        $income = Db::name('kuerp_sales_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))->find();
                        if($info_list_v['business_type']==3 && $info_list_v['type']==1){
                            //普通销售
                            $order = Db::name('kuerp_goods_sales')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();
                            if($order){
                                Db::name('kuerp_sales_payment_log')->insert((new Goods())->setDataInfo([
                                    'customers_id'   => $order['customers_id'],
                                    'business_type'  => '3',
                                    'bill_time'      => time(),
                                    'bill_code'      => $order['purchase_code'],
                                    'bill_codes'     => $info_list_v['purchase_code'],
                                    'pay_money'      => $info_list_v['write_off_price'],
                                    'reamrk'         => $list_v['remarks'],
                                    'createtime'     => time(),
                                ]));
                                $more_money = $order['real_money']-$info_list_v['write_off_price']-$order['pay_money'];
                                $status = $more_money<=0?1:2;
                                $pay_money = $order['pay_money']+$info_list_v['write_off_price'];
                                Db::name('kuerp_goods_sales')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))
                                    ->update(['pay_money'=>$pay_money,'pay_status'=>$status, 'account' => $account[0]['accounts_id'] ?? 0]);
                                Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['id'=>$info_list_v['id']]))->update([
                                    'old_money'=>$order['pay_money'],
                                    'old_status'=>$order['pay_status'],
                                    'old_status_i'=>$income['status']
                                ]);
                                if($income){
                                    Db::name('kuerp_sales_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))
                                        ->update(['status'=>$status]);
                                    Db::name('kuerp_sales_invoicing_log')->where(['bill_code'=>$info_list_v['purchase_number']])
                                        ->update(['status'=>$status]);
                                }
                            }
                        }
                        if($info_list_v['business_type']==3 && $info_list_v['type']==2){
                            //销售退货
                            $order = Db::name('kuerp_goods_sales_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();
                            if($order){
                                Db::name('kuerp_sales_payment_log')->insert((new Goods())->setDataInfo([
                                    'customers_id'   => $order['customers_id'],
                                    'business_type'  => '4',
                                    'bill_time'      => time(),
                                    'bill_code'      => $order['purchase_code'],
                                    'bill_codes'     => $info_list_v['purchase_code'],
                                    'pay_money'      => $info_list_v['write_off_price'],
                                    'reamrk'         => $list_v['remarks'],
                                    'createtime'     => time(),
                                ]));
                                $more_money = $order['real_money']+$info_list_v['write_off_price']-$order['retreat_money'];
                                $status = $more_money==0?1:2;
                                $pay_money = $order['retreat_money']+(-$info_list_v['write_off_price']);
                                Db::name('kuerp_goods_sales_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))
                                    ->update(['retreat_money'=>$pay_money,'retreat_status'=>$status]);
                                Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['id'=>$info_list_v['id']]))->update([
                                    'old_money'=>$order['retreat_money'],
                                    'old_status'=>$order['retreat_status'],
                                    'old_status_i'=>$income['status']
                                ]);
                                if($income){
                                    $status += 3;
                                    Db::name('kuerp_sales_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))
                                        ->update(['status'=>$status]);
                                    Db::name('kuerp_sales_invoicing_log')->where(['bill_code'=>$info_list_v['purchase_number']])
                                        ->update(['status'=>$status]);
                                }
                            }
                        }
                    }

                    if(!empty($accounts_receivable)){
                        Db::name('kuerp_accounts_receivable')->insertAll($accounts_receivable);
                    }

                    foreach ($accountData as $accountData_k=>$accountData_v){
                        if($accountData_v['balance']>0){
                            Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$accountData_v['account']]))->inc('balance',$accountData_v['balance'])->update();
                        }else if($accountData_v['balance']<0){
                            Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$accountData_v['account']]))->dec('balance',-$accountData_v['balance'])->update();
                        }
                    }
                    if(!empty($log)){
                        Db::name('kuerp_accounts_log')->insertAll($log);
                    }
                    Db::name('kuerp_customers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->update(['need_receive_account'=>$gave_money]);
                    $result = $this->model->where((new Goods())->setWhere(['id'=>$list_v['id'],'examine_status'=>'0']))->update(['examine_status'=>1,'examine_id'=>$admin['id'],'examine_time'=>time(),'operation_id'=>$admin['id']]);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if(isset($result) && $result !== false){
                    $success_count += 1;
                }
            }
            if($success_count>0){
                if($examine_set!=1){
                    return ['status'=>1,'msg'=>'操作成功'];
                }
                $this->success($success_count.'条数据审核成功');
            }else{
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'操作失败'];
                }
                $this->error('审核失败');
            }
        }else{
            $list = Db::name('kuerp_collection')->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'1']))->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);
            }
            if(empty($list)){
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'系统错误'];
                }
                $this->error('请选择需要反审核的数据');
            }
            $success_count = 0;
            foreach ($list as $list_k=>$list_v){
                //账户信息
                $accountData = [];$log = [];
                $account = Db::name('kuerp_collection_account')->where((new Goods())->setWhere(['collection_id'=>$list_v['id']]))->select();
                foreach ($account as $account_k=>$account_v){
                    $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$account_v['accounts_id']]))->find();
                    $accountData[] = [
                        'balance'  => $account_v['money'],
                        'account'  => $accounts['id'],
                    ];
                    $log[] = $account_v['purchase_code'];
                }
                //账户信息结束
                $info_list = Db::name('kuerp_collection_sale')->where((new Goods())->setWhere(['collection_id'=>$list_v['id']]))->select();
                $suppliers = Db::name('kuerp_customers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->find();
                $gave_money = $suppliers['need_receive_account'] + $list_v['money'];
                Db::startTrans();
                try {
                    Db::name('kuerp_accounts_receivable')->where(['purchase_code'=>$list_v['purchase_code'],'business_type'=>'9'])->delete();
                    Db::name('kuerp_writeoff_source')->where(['purchase_code'=>$list_v['purchase_code'],'purchase_type'=>'1','type'=>'1'])->delete();
                    Db::name('kuerp_customers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->update(['need_receive_account'=>$gave_money]);

                    foreach ($info_list as $info_list_k=>$info_list_v){
                        if($info_list_v['business_type']==3 && $info_list_v['type']==1){
                            //普通购货
                            $order = Db::name('kuerp_goods_sales')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();
                            if($order){
                                Db::name('kuerp_goods_sales')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))
                                    ->update(['pay_money'=>$info_list_v['old_money'],'pay_status'=>$info_list_v['old_status']]);
                            }
                        }
                        if($info_list_v['business_type']==3 && $info_list_v['type']==2){
                            //购货退货
                            $order = Db::name('kuerp_goods_sales_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();
                            if($order){
                                Db::name('kuerp_goods_sales_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))
                                    ->update(['retreat_money'=>$info_list_v['old_money'],'retreat_status'=>$info_list_v['old_status']]);
                            }
                        }
                        $income = Db::name('kuerp_sales_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))->find();
                        if($income){
                            if($info_list_v['business_type']==3 && $info_list_v['type']==2){
                                $info_list_v['old_status'] += 3;
                            }
                            Db::name('kuerp_sales_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))
                                ->update(['status'=>$info_list_v['old_status']]);
                            Db::name('kuerp_sales_invoicing_log')->where(['bill_code'=>$info_list_v['purchase_number']])
                                ->update(['status'=>$info_list_v['old_status']]);
                        }
                        Db::name('kuerp_sales_payment_log')->where((new Goods())->setWhere(['bill_codes'=>$info_list_v['purchase_code']]))->delete();
                    }
                    foreach ($accountData as $accountData_k=>$accountData_v){
                        if($accountData_v['balance']>0){
                            Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$accountData_v['account']]))->dec('balance',$accountData_v['balance'])->update();
                        }else if($accountData_v['balance']<0){
                            Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$accountData_v['account']]))->inc('balance',-$accountData_v['balance'])->update();
                        }
                    }
                    if(!empty($log)){
                        Db::name('kuerp_accounts_log')->where((new Goods())->setWhere(['bill_code'=>['in',$log],'business_type'=>'7']))->delete();
                    }
                    $result = $this->model->where((new Goods())->setWhere(['id'=>$list_v['id'],'examine_status'=>'1']))->update(['examine_status'=>'0','examine_id'=>null,'examine_time'=>null,'operation_id'=>$admin['id']]);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if(isset($result) && $result !== false){
                    $success_count += 1;
                }
            }
            if($success_count>0){
                if($examine_set!=1){
                    return ['status'=>1,'msg'=>'操作成功'];
                }
                $this->success($success_count.'条数据反审核成功');
            }else{
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'操作失败'];
                }
                $this->error('反审核失败');
            }
        }
    }

}
